<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<link rel="stylesheet" href="css/order.css" />
	<link rel="stylesheet" href="css/form.css" />
</head>

<body>
	<div id="header-wrap" data-mn="0" data-limit=2>
		<a href="###" data-link="buy" data-type="ico" class="left ico-m ico-left"></a>
		<h2>投资订单</h2>
		<a href="###" data-link="recharge" data-type="ico" class="right btn b-small b-green">充值</a>
	</div>
	<div id="order">
		<h2>购买<b id="pn"></b><span id="ps" style="font-size: 14px;font-weight: normal; color: #999"></span></h2>
		<div class="tips">
			<div class="t-error"></div>
		</div>
		<ul class="label-list">
			<li id="pre_get">
				<label>万份收益(元)</label>
				<div class="ll-cont"><span></span></div>
			</li>
			<li id="yearly">
				<label>年化收益率(%)</label>
				<div class="ll-cont"><span>8.00</span></div>
			</li>
			<li class="">
				<label>首次收益时间</label>
				<div class="ll-cont"><span>2015-04-28</span></div>
			</li>
			<li class="">
				<label>单价(元/份)</label>
				<div class="ll-cont"><span>1.00</span></div>
			</li>
			<li class="">
				<label>手续费(元)</label>
				<div class="ll-cont"><span>0.00</span></div>
			</li>
			<li id="xg">
				<label>单笔限购(份)</label>
				<div class="ll-cont"><span>10000</span></div>
			</li>
			<li id="xc">
				<label>投资上限(元)</label>
				<div class="ll-cont"><span>100000.00</span></div>
			</li>
			<li id="keep">
				<label>当前持有(份)</label>
				<div class="ll-cont"><span></span></div>
			</li>
			<li id="kc">
				<label>当前库存份)</label>
				<div class="ll-cont"><span></span></div>
			</li>
		</ul>
		<div class="fixed order-submit">
			<div class="yu">可用余额(元)：<b id="balance"></b></div>
			<div class="btn-box">
				<input type="number" id="ac" placeholder="请输入购买份额" />
				<a href="###" data-link="order-feed" class="buy-btn"><span id="buy">购买</span></a>
				<a href="###" id="max" class="max-btn">MAX</a>
			</div>
		</div>
	
	</div>
	<script>
	$(function(){
		var pn = $.cookie("pn_temp")
		var ps = $.cookie("ps_temp")
		var balance = Number($.cookie("balance"));
		var balance = $.cookie("balance")
		var balance_show = parseFloat(balance).toFixed(2)
		
		$("#pn").text(" "+pn)
		$("#ps").text(" ("+ps+")");
		$("#balance").text(balance_show)
		
		max_buy = function(keep,kc,xc,xg){
			var max;
			if(keep>=xc){
				max = 0
				return false
			}else{
				max = xc -keep
				if(max > xg){
					max = xg
					if(max > kc ){
						max =kc
					}
				}
				return max
			}
		}
		if(pn == "富贵天天"){
			var keep = Number($.cookie("keep_n"));//持有
			var kc = Number($.cookie("kc_n"));//库存
			var xc = Number($("#xc span").text());//限持
			var xg = Number($("#xg span").text());//单次限购
			$("#pre_get label").text("每天每万份收益(元)");
			$("#pre_get span").text("3.33");
			$("#yearly span").text("12.00");
			$("#keep span").text(keep)
			$("#kc span").text(kc)
			$("#max").on("click",function(){
				var max = max_buy(keep,kc,xc,xg)
				$("#ac").val(max)
			})
			$("#ac").on("keyup",function(){
				var val = parseInt($(this).val(),10)
				$(this).val(val);
				val = $(this).val();
				if(isNaN(val[0])){
					$(this).val("")
				}
			})
			$("#buy").on("click",function(){
				var ac = $("#ac").val();
				var max = max_buy()
				var ac_return = $("#ac").check({type:8});
				if(ac == ''){
					$(".t-error").text("购买份额不能为空").fadeIn(100)
					reset_tip()
					return false
				}else if(ac > max){
					$(".t-error").text("最多购买 "+max+" 份").fadeIn(100)
					reset_tip()
					return false
				}else if (ac_return == 0){
					$(".t-error").text("请输入有效份额").fadeIn(100)
					reset_tip()
					return false
				}else{
					balance -= ac;
					keep = parseInt(keep) + parseInt(ac);
					kc -= ac;
					$.cookie("balance",balance);
					$.cookie("ac_temp",ac);
					$.cookie("keep_n",keep)
					$.cookie("kc_n",kc)
				}
				
			})
		}else if(pn == "富贵月月"){
			var lock = parseInt(ps.match(/^\d/));//锁定期
			var keep = Number($.cookie("keep_"+lock));//持有
			var kc = Number($.cookie("kc_"+lock));//库存
			var xc = Number($("#xc span").text());//限持
			var xg = Number($("#xg span").text());//单次限购
			pre_get = function(lo,w){
				var yearly;
				switch (lo){
					case 1:
					yearly = 0.08;
					break;
					case 3:
					yearly = 0.1;
					break;
					case 6:
					yearly = 0.12;
					break;
					case 12:
					yearly = 0.14;
					break;
				}
				var pg = parseFloat(10000*yearly/360*30*lo).toFixed(2)
				yearly = yearly*100+".00%"
				if(w == 1){
					return pg;
				}else if(w==2){
					return yearly;
				}
			}
			$("#pre_get label").text("每万份预期收益(元)");
			$("#pre_get span").text(pre_get(lock,1));
			$("#yearly span").text(pre_get(lock,2));
			$("#keep span").text(keep)
			$("#kc span").text(kc)
			$("#max").on("click",function(){
				var max = max_buy(keep,kc,xc,xg)
				$("#ac").val(max)
			})
			$("#ac").on("keyup",function(){
				var val = parseInt($(this).val(),10)
				$(this).val(val);
				val = $(this).val();
				if(isNaN(val[0])){
					$(this).val("")
				}
			});
			
			$("#buy").on("click",function(){
				var ac = $("#ac").val();
				var max = max_buy()
				var ac_return = $("#ac").check({type:8});
				if(ac == ''){
					$(".t-error").text("购买份额不能为空").fadeIn(100)
					reset_tip()
					return false
				}else if(ac > max){
					$(".t-error").text("最多购买 "+max+" 份").fadeIn(100)
					reset_tip()
					return false
				}else if (ac_return == 0){
					$(".t-error").text("请输入有效份额").fadeIn(100)
					reset_tip()
					return false
				}else{
					balance -= ac;
					keep = parseInt(keep) + parseInt(ac);
					kc -= ac;
					//生成购买记录
					var oid = time_a()+random_code(5)
					var yo = $.cookie("yo");
					if(yo == undefined){
						$.cookie("yo",lock+","+ac+","+oid)//更新当前库存
					}else{
						yo_len = yo.split("/").length;
						if(yo_len<5){
							$.cookie("yo",yo+"/"+lock+","+ac+","+oid)
						}else{
							$(".t-error").text("DEMO版只允许用户存储5个富贵月月的订单").fadeIn(100)
							reset_tip()
							return false
						}
						
					}
					$.cookie("balance",balance);//当前余额
					$.cookie("ac_temp",ac);//记录消耗钱数（购买份额）
					$.cookie("keep_"+lock,keep)//更新当前持有量
					$.cookie("kc_"+lock,kc)//更新当前库存
					
					
				}
				
			})
		}
		
	})
	</script>	
</body>

</html>
